package com.xingqing.controller;

import com.xingqing.domain.entity.Dept;
import com.xingqing.domain.vo.Result;
import com.xingqing.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @GetMapping
    public Result<List<Dept>> list() {
        log.info("查询所有部门信息");
        List<Dept> deptList = deptService.list();
        log.info("查询到{}个部门", deptList.size());
        return Result.success(deptList);
    }

    @GetMapping("/{id}")
    public Result<Dept> getById(@PathVariable Long id) {
        log.info("根据ID查询部门信息，ID: {}", id);
        Dept dept = deptService.getById(id);
        log.info("查询到部门信息: {}", dept != null ? dept.getName() : "null");
        return Result.success(dept);
    }

    @PostMapping
    public Result<Void> save(@RequestBody Dept dept) {
        log.info("新增部门: {}", dept.getName());
        deptService.save(dept);
        log.info("部门新增成功");
        return Result.success();
    }

    @PutMapping("/{id}")
    public Result<Void> update(@PathVariable Long id, @RequestBody Dept dept) {
        log.info("更新部门信息，ID: {}, 新名称: {}", id, dept.getName());
        dept.setId(id);
        deptService.updateById(dept);
        log.info("部门更新成功");
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        log.info("删除部门，ID: {}", id);
        deptService.deleteById(id);
        log.info("部门删除成功");
        return Result.success();
    }
}